<?php


use App\Library\Utils\Arr as ArrUtils;
use Phinx\Seed\AbstractSeed;
use Faker\Factory as FakerFactory;

class ChapterLikeSeeder extends AbstractSeed
{
    public function getDependencies(): array
    {
        return [
            'ChapterSeeder',
            'UserSeeder',
        ];
    }

    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * https://book.cakephp.org/phinx/0/en/seeding.html
     */
    public function run()
    {
        $chapters = $this->fetchAll("SELECT * FROM kg_chapter");
        $users = $this->fetchAll("SELECT * FROM kg_user");

        $faker = FakerFactory::create('zh_CN');

        for ($j = 0; $j < 200; $j++) {
            $cid = ArrUtils::rand($chapters)['id'];
            $likeCount = rand(0, 50);
            for ($i = 0; $i < $likeCount; $i++) {
                $time = $faker->unixTime;
                $data = [
                    'chapter_id' => $cid,
                    'user_id' => ArrUtils::rand($users)['id'],
                    'create_time' => $time,
                    'update_time' => $time,
                ];
                $this->table('kg_chapter_like')->insert([$data])->saveData();
            }
        }
    }

}
